package com.qfedu.service.impl;

import com.qfedu.dao.IAddressDao;
import com.qfedu.dao.IOrderDao;
import com.qfedu.dao.impl.AddressDaoImpl;
import com.qfedu.dao.impl.OrderDaoImpl;
import com.qfedu.entity.Address;
import com.qfedu.entity.Order;
import com.qfedu.entity.Result;
import com.qfedu.service.IOrderService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements IOrderService {
    private IOrderDao orderDao = new OrderDaoImpl();
    private IAddressDao addressDao = new AddressDaoImpl();
    @Override
    public Result queryOrderList(int uid) {
        List<Order> orders = orderDao.queryOrderList(uid);
        return new Result("",true,orders);
    }

    @Override
    public Result updateMoney(int uid, int id) {
        int i = orderDao.updateMoney(uid, id);
        if (i==0){
            return new Result("异常",false);
        }else {
            return new Result("正常",true);
        }
    }

    @Override
    public List<String> getDetail(int uid) {
        List<Address> addressList = addressDao.queryAddressList(uid);
        List<String> details = new ArrayList<String>();
        for (Address address : addressList) {
            details.add("收件人:"+address.getName()+" "+"联系方式:"+address.getPhone()+" "+"地址:"+address.getDetail());
        }
        return details;
    }

    @Override
    public Result insertOrder(int uid, int aid, double money) {
        long id = System.currentTimeMillis();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        System.out.println(format);
        int i = orderDao.insertOrder(id + "", uid, aid, format, money);
        if (i==0){
            return new Result("插入失败",false);
        }else {
            return new Result("插入成功",true);
        }
    }

    @Override
    public Result queryOrder(int id) {
        Order order = orderDao.queryOrder(id);
        if (null==order){
            return new Result("查询失败",false);
        }else {
            return new Result("查询成功",true,order);
        }
    }

    @Override
    public Order findOrderList(String oid) {
        return orderDao.findOrder(oid);
    }

//    public static void main(String[] args) {
//        OrderServiceImpl orderService = new OrderServiceImpl();
//        Result result = orderService.queryOrderList(2);
//        System.out.println(result);
//    }
}
